package com.mcflies.commons;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.mcflies.bean.GridViewBean;
import com.mcflies.bean.ListViewBean;

/**
 * @author:mcfly_jun
 * @create_date:2012-8-31
 * @comment:公共处理类
 */
public class CommonsUtil {
	/**
	 * @param dataArray
	 * @param assetManager
	 * @param imageFolder
	 * @return
	 * @commont:根据传入的dataArray,分解并组装GridViewItem所需要的数据
	 */
	public static List<GridViewBean> getGridViewData(String[] dataArray,
			AssetManager assetManager, String imageFolder) {
		List<GridViewBean> beanList = new ArrayList<GridViewBean>();
		for (int i = 0; i < dataArray.length; i++) {
			String tempItemStr = dataArray[i];
			String[] tempItemArray = tempItemStr.split("@");
			if (tempItemArray.length != 3) {// 目前控制为三个
				continue;
			}

			GridViewBean bean = new GridViewBean();
			// 菜单标题
			String tempMenuTitle = tempItemArray[0];
			bean.setTitle(tempMenuTitle);
			// 菜单图片
			String tempMenuImageName = tempItemArray[1];
			if (!(tempMenuImageName.endsWith(".jpg")
					|| tempMenuImageName.endsWith(".png") || tempMenuImageName
						.endsWith(".ico"))) {// 只支持.jpg、.png和.ico格式的图片文件
				tempMenuImageName = "default.png";
			}
			InputStream is = null;
			try {
				is = assetManager.open(imageFolder + File.separatorChar
						+ tempMenuImageName);
				if (null == is) {
					is = assetManager.open(imageFolder + File.separatorChar
							+ "default.png");
				}
			} catch (Exception e) {
				Log.i(McfliesConstant.MCFLIES_EXCEPTION_INFO_FLAG,
						"CommonsUtil.java&&getGridViewData()方法异常："
								+ e.getMessage());
			}
			Bitmap tempMenuImage = BitmapFactory.decodeStream(is);
			bean.setBitmap(tempMenuImage);

			// 点击菜单目标Activity
			String tempMenuIntent = tempItemArray[2];
			bean.setIntent(tempMenuIntent);
			beanList.add(bean);
		}
		return beanList;
	}

	/**
	 * @param dataArray
	 * @param assetManager
	 * @param imageFolder
	 * @return
	 * @commont:根据传入的dataArray,分解并组装ListViewItem所需要的数据
	 */
	public static List<ListViewBean> getListViewData(String[] dataArray,
			AssetManager assetManager, String imageFolder) {
		List<ListViewBean> beanList = new ArrayList<ListViewBean>();
		for (int i = 0; i < dataArray.length; i++) {
			String tempItemStr = dataArray[i];
			String[] tempItemArray = tempItemStr.split("@");
			if (tempItemArray.length != 3) {// 目前控制为三个
				continue;
			}

			ListViewBean bean = new ListViewBean();
			// 菜单标题
			String tempMenuTitle = tempItemArray[0];
			bean.setTitle(tempMenuTitle);
			// 菜单图片
			String tempMenuImageName = tempItemArray[1];
			if (!(tempMenuImageName.endsWith(".jpg")
					|| tempMenuImageName.endsWith(".png") || tempMenuImageName
						.endsWith(".ico"))) {// 只支持.jpg、.png和.ico格式的图片文件
				tempMenuImageName = "default.png";
			}
			InputStream is = null;
			try {
				is = assetManager.open(imageFolder + File.separatorChar
						+ tempMenuImageName);
				if (null == is) {
					is = assetManager.open(imageFolder + File.separatorChar
							+ "default.png");
				}
			} catch (Exception e) {
				Log.i(McfliesConstant.MCFLIES_EXCEPTION_INFO_FLAG,
						"CommonsUtil.java&&getListViewData()方法异常："
								+ e.getMessage());
			}
			Bitmap tempMenuImage = BitmapFactory.decodeStream(is);
			bean.setBitmap(tempMenuImage);

			// 点击菜单目标Activity
			String tempMenuIntent = tempItemArray[2];
			bean.setIntent(tempMenuIntent);
			beanList.add(bean);
		}
		return beanList;
	}
}
